<template>
    <div class="app-container">

        <!-- 查询条件 -->
        <el-card>
            <el-form ref="logininforRef" :inline="true" :model="queryParams" label-width="68px">

                <el-form-item label="登录地址" prop="ipaddr">
                    <el-input v-model="queryParams.ipaddr" placeholder="请输入登录地址" clearable style="width: 240px" />
                </el-form-item>

                <el-form-item label="用户名称" prop="userName">
                    <el-input v-model="queryParams.userName" placeholder="请输入用户名称" clearable style="width: 240px" />
                </el-form-item>

                <el-form-item label="状态" prop="status">
                    <el-select v-model="queryParams.status" placeholder="登录状态" clearable>
                        <el-option v-for="dict in sys_common_status" :key="dict.value" :label="dict.label"
                            :value="dict.value" />
                    </el-select>
                </el-form-item>

                <el-form-item label="登录时间" style="width: 308px">
                    <el-date-picker v-model="dateRange" value-format="YYYY-MM-DD" type="daterange" range-separator="-"
                        start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                    <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>

        <!-- 表格 结果展示 -->
        <el-card style="margin-top: 20px;">

            <template #header>
                <el-row :gutter="10">
                    <el-col :span="1.5">
                        <el-button v-hasPermi="['monitor:logininfor:remove']" type="danger" plain icon="Delete"
                            :disabled="multiple" @click="handleDelete()">删除</el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button v-hasPermi="['monitor:logininfor:remove']" type="danger" plain icon="Delete"
                            @click="handleClean">清空</el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button v-hasPermi="['monitor:logininfor:unlock']" type="primary" plain icon="Unlock"
                            :disabled="single" @click="handleUnlock">解锁</el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button v-hasPermi="['monitor:logininfor:export']" type="warning" plain icon="Download"
                            @click="handleExport">导出</el-button>
                    </el-col>
                    <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
                </el-row>
            </template>

            <el-table v-loading="loading" :data="pageData.rows" style="width: 100%"
                @selectionChange="handleSelectionChange">
                <el-table-column type="selection" align="center" width="55" />
                <el-table-column label="访问编号" align="center" prop="infoId" />
                <el-table-column label="用户名称" align="center" prop="userName" />
                <el-table-column label="地址" align="center" prop="ipaddr" :show-overflow-tooltip="true" />
                <el-table-column label="登录地点" align="center" prop="loginLocation" :show-overflow-tooltip="true" />
                <el-table-column label="操作系统" align="center" prop="os" :show-overflow-tooltip="true" />
                <el-table-column label="浏览器" align="center" prop="browser" :show-overflow-tooltip="true" />
                <el-table-column label="登录状态" align="center" prop="status">
                    <template #default="scope">
                        <dict-tag :options="sys_common_status" :value="scope.row.status" />
                    </template>
                </el-table-column>
                <el-table-column label="描述" align="center" prop="msg" :show-overflow-tooltip="true" />
                <el-table-column label="访问时间" align="center" prop="loginTime" width="180">
                    <template #default="scope">
                        <span>{{parseTime(scope.row.loginTime)}}</span>
                    </template>
                </el-table-column>
            </el-table>

            <pagination v-show="pageData.total > 0" v-model:page="queryParams.pageNum"
                v-model:limit="queryParams.pageSize" :total="pageData.total" @pagination="getList" />
        </el-card>

    </div>
</template>

<script setup name="Logininfor" lang="ts">
    import { ref, reactive, toRefs, getCurrentInstance, ComponentInternalInstance } from 'vue';
    import { SysLoginInforQueryReq, SysLoginInforResp } from '@/types/monitor/logininfor';
    import { list, delLogininfor } from '@/api/monitor/logininfor';
    import { parseTime } from '@/utils/ruoyi';
    // 获取当前vue3的页面this对象转换为proxy代理，查询 sys_dict_data表中的值
    const { proxy } = getCurrentInstance() as ComponentInternalInstance;
    const { sys_common_status } = proxy!.useDict('sys_common_status');

    // 响应式对象
    const dateRange = ref<any[]>([]);
    const loading = ref<boolean>(false);

    // 解锁按钮 默认不启用
    const single = ref<boolean>(true);
    // 删除按钮 默认不启用
    const multiple = ref<boolean>(true);
    const showSearch = ref<boolean>(true);
    // 表格选中的项
    const mutilSelection = ref<SysLoginInforResp[]>([]);
    // 表格选中项 的主键
    const ids = ref<number[]>([]);

    // 分页数据
    const pageData = reactive<{
        total : number;
        rows : SysLoginInforResp[]
    }>({
        total: 0,
        rows: []
    })

    // 响应式代理模型
    const data = reactive<{
        queryParams : SysLoginInforQueryReq;
    }>({
        queryParams: {
            userName: '',
            ipaddr: '',
            status: '',
            pageNum: 1,
            pageSize: 10
        }
    })

    // 代理模型转为相应模型
    const { queryParams } = toRefs(data)

    // 重置
    function resetQuery() {
        dateRange.value = [];
        proxy?.resetForm('logininforRef');
        handleQuery();
    }

    // 查询
    function handleQuery() {
        queryParams.value.pageNum = 1;
        getList();
    }

    // 分页查询
    function getList() {
        loading.value = true;

        // 添加日期值
        list(proxy?.addDateRange(queryParams.value, dateRange.value)).then((resp : any) => {
            pageData.total = resp.data.total;
            pageData.rows = resp.data.list;
            loading.value = false;
        });
    }

    // 多选框选中数据
    function handleSelectionChange(selection : SysLoginInforResp[]) {
        mutilSelection.value = selection;
        ids.value = selection.map(row => row.infoId);
        // 判断 解锁按钮禁用
        single.value = selection.length !== 1;
        // 判断 删除按钮禁用
        multiple.value = selection.length === 0;
    }

    // 删除
    function handleDelete(row ?: SysLoginInforResp) {
        let primaryKeys : any;
        if (row) {
            primaryKeys = row.infoId;
        } else {
            primaryKeys = ids.value;
        }

        proxy?.$modal
            .confirm('是否确认删除参数编号为"' + primaryKeys + '"的数据项？')
            .then(function () {
                return delLogininfor(primaryKeys);
            })
            .then(() => {
                getList();
                proxy!.$modal.msgSuccess('删除成功');
            })
            .catch((e : any) => {
                console.log(e);
            });
    }

    // 清空
    function handleClean() {

    }

    // 解锁
    function handleUnlock() {

    }

    // 导出
    function handleExport() {
        let queryData : any = {};
        // 根据勾选行下载
        if (ids.value && ids.value.length > 0) {
            queryData.ids = ids.value.join();
        } else {
            queryData = {
                ...queryParams.value
            };
        }

        proxy?.download(
            'admin/logininfor/export',
            queryData,
            `logininfor_${new Date().getTime()}.xlsx`
        );
    }

    // 初始化查询
    getList()
</script>
